package club.banyuan.blog.demo;

import club.banyuan.blog.dto.Weather;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

public class HttpClientDemo {


    public static String get(String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();

        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    public static void main(String[] args) throws IOException {

        // 示例接口 http://119.45.149.229:8080/api/tag/query?kw=j

        String url = "http://www.weather.com.cn/data/cityinfo/101190101.html";
        String json = get(url);
        System.out.println(json);

        JSONObject obj = JSONUtil.parseObj(json);
        JSONObject weatherinfo = (JSONObject) obj.get("weatherinfo");

        Weather weather = new Weather();
        weather.setCityName((String) weatherinfo.get("city"));
        weather.setMinTemp((String) weatherinfo.get("temp1"));
        weather.setMaxTemp((String) weatherinfo.get("temp2"));
        weather.setWeather((String) weatherinfo.get("weather"));

    }
}
